﻿using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HFK.DxFluentUI.Utility;
using HFK.DxFluentUITest.Dummy.DataObjects;
using MbUnit.Framework;

namespace HFK.DxFluentUITest.Utility
{
  [TestFixture]
  public class TypedListTestFixture
  {
    [Test]
    public void TestGetItemPropertiesNoRegisteredProperties()
    {
      TypedList<SimpleClass> typedList = new TypedList<SimpleClass>();

      PropertyDescriptorCollection propertyDescriptorList = (typedList as ITypedList).GetItemProperties(null);

      Assert.AreEqual(4, propertyDescriptorList.Count);
      CollectionAssert.AreEquivalent(
        new string[] { "DateValue", "IntValue", "StringValue", "ShcData" }, 
        (from pd in new EnumerableSource<PropertyDescriptor>(propertyDescriptorList) select pd.Name).ToList());
    }

    [Test]
    public void TestGetItemPropertiesWithRegisteredProperties_DescriptorTest()
    {
      TypedList<SimpleClass> typedList = new TypedList<SimpleClass>();
      typedList.RegisterProperty("ShcData.StringValue");
      typedList.RegisterProperty("ShcData.DhcProperty");
      typedList.RegisterProperty("ShcData.DhcProperty.ThcProperty");
      typedList.RegisterProperty("ShcData.DhcProperty.ThcProperty.StringProperty");

      PropertyDescriptorCollection propertyDescriptorList = (typedList as ITypedList).GetItemProperties(null);

      Assert.AreEqual(8, propertyDescriptorList.Count);
      CollectionAssert.AreEquivalent(
        new string[] { 
          "DateValue", "IntValue", "StringValue", "ShcData"
          , "ShcData.StringValue", "ShcData.DhcProperty",
          "ShcData.DhcProperty.ThcProperty",
          "ShcData.DhcProperty.ThcProperty.StringProperty"}, 
        (from pd in new EnumerableSource<PropertyDescriptor>(propertyDescriptorList) select pd.Name).ToList());
    }

    [Test]
    public void TestGetItemPropertiesWithRegisteredProperties_ValueTest()
    {
      TypedList<SimpleClass> typedList = new TypedList<SimpleClass>();
      typedList.RegisterProperty("ShcData.StringValue");
      typedList.RegisterProperty("ShcData.DhcProperty");
      typedList.RegisterProperty("ShcData.DhcProperty.ThcProperty");
      typedList.RegisterProperty("ShcData.DhcProperty.ThcProperty.StringProperty");

      PropertyDescriptorCollection propertyDescriptorList = (typedList as ITypedList).GetItemProperties(null);

      const int testScInt = 1;
      const string testScString = "TestScString";
      SimpleClass sc = new SimpleClass();
      sc.IntValue = testScInt;
      sc.StringValue = testScString;

      const string testShcString = "TestShcString";
      SingleHopClass shc = new SingleHopClass();
      shc.StringValue = testShcString;

      const string testThcString = "TestThcString";
      TripleHopClass thc = new TripleHopClass() { StringProperty = testThcString };

      shc.DhcProperty = new DualHopClass() { ThcProperty = thc };
      sc.ShcData = shc;

      Assert.AreEqual(testScInt, propertyDescriptorList["IntValue"].GetValue(sc));
      Assert.AreEqual(testScString, propertyDescriptorList["StringValue"].GetValue(sc));
      Assert.AreSame(shc, propertyDescriptorList["ShcData"].GetValue(sc));
      Assert.AreEqual(testShcString, propertyDescriptorList["ShcData.StringValue"].GetValue(sc));
      Assert.AreEqual(testThcString, propertyDescriptorList["ShcData.DhcProperty.ThcProperty.StringProperty"].GetValue(sc));

    }
  }
}
